

Imports System.Reflection.Assembly
Imports System.Runtime.InteropServices
Imports Microsoft.Office.Core
Imports Microsoft.Office.Interop.Access
Imports System.Reflection
Imports Microsoft.Office.Interop


Namespace ModObjects

	'returns a collection of access objects that have been modified since a specified time
	Public Class AccessModifiedObjectsProvider

		Private _lastModificationDate As DateTime

		Const GET_MOD_OBJECTS_SQL As String = "SELECT MSysObjects.Name, MSysObjects.DateCreate, " & _
		  "MSysObjects.DateUpdate, " & _
		  "IIf(type=1,'Table',IIf(type=6,'Linked Table',IIf(type=5,'Query',IIf(type=-32768,'Form',IIf(type=-32764,'Report',IIf(type=-32766,'Module',IIf(type=-32761,'Module','Unknown'))))))) AS ObjectType " & _
		  " FROM MSysObjects WHERE (((MSysObjects.Type) In (1,5,6,-32768,-32764,-32766,-32761)) AND ((Left([Name],4))<>'MSys') AND ((Left([Name],1))<>'~')) " & _
		  "  and DateUpdate > {0}" & _
		  " ORDER BY MSysObjects.Name"

		Public Sub setLastModificationDate(ByVal v As DateTime)
			Me._lastModificationDate = v
		End Sub

        Public Function getModifiedObjects(ByVal accessAppl As Access.Application) _
          As System.Collections.Generic.List(Of AccessModifiedObject)

            Dim ret As New System.Collections.Generic.List(Of AccessModifiedObject)
            Dim curdb As dao.Database = accessAppl.CurrentDb
            Dim sql As String = String.Format(GET_MOD_OBJECTS_SQL, quoteDateTime(Me._lastModificationDate))
            Dim d As dao.Recordset = curdb.Connection.OpenRecordset(sql)

            While Not d.EOF
                Dim t As New AccessModifiedObject
                t.objModifiedTime = CDate(d("DateUpdate").Value)
                t.objName = CStr(d("Name").Value)
                t.objType = CType(CStr(d("ObjectType").Value), AcObjectType)
                d.MoveNext()
            End While

            Return ret

        End Function

		private Function quoteDateTime(dt As datetime) As String

            return  "#" & Microsoft.VisualBasic.Strings.format(dt, "dd-MMMM-yyyy HH:mm:ss") & "#"
    
		End Function

	End Class


End Namespace
